﻿using System;
using System.Web.UI;

namespace PickerControl
{
	public partial class PickerControl : UserControl
	{
		#region Constants

		private const string OPEN_PICKER_JS = "{0}(); return false;";

		private const string OPEN_PICKER_FUNCTION_NAME = "openPicker";
		private const string PICKER_WINDOW_NAME = "choosePicker";
		private const string SET_VALUE_FUNCTION_NAME = "setValue";

		#endregion Constants

		#region Properties

		public string PickerUrl { get; set; }

		protected string OpenPickerFunctionName
		{
			get { return GetUniqueName(OPEN_PICKER_FUNCTION_NAME); }
		}

		protected string PickerWindowName
		{
			get { return GetUniqueName(PICKER_WINDOW_NAME); }
		}

		protected string SetValueFunctionName
		{
			get { return GetUniqueName(SET_VALUE_FUNCTION_NAME); }
		}

		#endregion Properties

		#region Event handlers

		protected void Page_Load(object sender, EventArgs e)
		{
			string openPickerJS = string.Format(OPEN_PICKER_JS, OpenPickerFunctionName);
			openPickerButton.Attributes["onclick"] = openPickerJS;
		}

		#endregion Event handlers

		#region Helpers

		private string GetUniqueName(string name)
		{
			return string.Format("{0}_{1}", name, ClientID);
		}

		#endregion Helpers
	}
}